<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>朋友列表 - 朋友管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f7fa;
            color: #333;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo-section {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .logo {
            width: 40px;
            height: 40px;
            filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
            background: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 18px;
            color: white;
        }
        
        .logo:before {
            content: "朋";
            display: block;
        }
        
        .header h1 {
            font-size: 24px;
            font-weight: 500;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .user-name {
            font-size: 16px;
        }
        
        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .main-content {
            max-width: 1200px;
            margin: 30px auto;
            padding: 0 20px;
        }
        
        .toolbar {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .search-box {
            display: flex;
            gap: 10px;
            flex: 1;
            max-width: 400px;
        }
        
        .search-input {
            flex: 1;
            padding: 10px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 6px;
            font-size: 14px;
        }
        
        .search-input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .search-btn {
            background: #667eea;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: background 0.3s ease;
        }
        
        .search-btn:hover {
            background: #5a6fd8;
        }
        
        .filter-select {
            padding: 10px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 6px;
            font-size: 14px;
            background: white;
        }
        
        .add-btn {
            background: #27ae60;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            transition: background 0.3s ease;
        }
        
        .add-btn:hover {
            background: #229954;
        }
        
        .friends-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }
        
        .friend-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        
        .friend-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
        }
        
        .friend-header {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .friend-name {
            font-size: 18px;
            font-weight: 600;
            color: #333;
        }
        
        .friend-info {
            margin-bottom: 15px;
        }
        
        .info-item {
            display: flex;
            margin-bottom: 8px;
            font-size: 14px;
        }
        
        .info-label {
            color: #666;
            min-width: 60px;
            font-weight: 500;
        }
        
        .info-value {
            color: #333;
            flex: 1;
        }
        
        .friend-actions {
            display: flex;
            gap: 10px;
        }
        
        .action-btn {
            flex: 1;
            padding: 8px 12px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
            transition: all 0.3s ease;
            text-decoration: none;
            text-align: center;
        }
        
        .view-btn {
            background: #3498db;
            color: white;
        }
        
        .view-btn:hover {
            background: #2980b9;
        }
        
        .edit-btn {
            background: #f39c12;
            color: white;
        }
        
        .edit-btn:hover {
            background: #e67e22;
        }
        
        .delete-btn {
            background: #e74c3c;
            color: white;
        }
        
        .delete-btn:hover {
            background: #c0392b;
        }
        
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #666;
        }
        
        .empty-state h3 {
            font-size: 24px;
            margin-bottom: 15px;
            color: #333;
        }
        
        .empty-state p {
            font-size: 16px;
            margin-bottom: 20px;
        }
        
        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
        }
        
        @media (max-width: 768px) {
            .toolbar {
                flex-direction: column;
                align-items: stretch;
            }
            
            .search-box {
                max-width: none;
            }
            
            .friends-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <div class="logo-section">
                <img src="/static/images/logo.svg" alt="朋友管理系统" class="logo">
                <h1>朋友管理系统</h1>
            </div>
            <div class="user-info">
                <span class="user-name" id="userName">用户</span>
                <button class="logout-btn" onclick="logout()">退出登录</button>
            </div>
        </div>
    </div>
    
    <div class="main-content">
        <div class="toolbar">
            <div class="search-box">
                <input type="text" class="search-input" id="searchInput" placeholder="搜索朋友姓名...">
                <button class="search-btn" onclick="searchFriends()">搜索</button>
            </div>
            
            <a href="/friend/create" class="add-btn">添加朋友</a>
            <a href="/couple/list" class="add-btn" style="background: #e91e63;">💕 情侣管理</a>
        </div>
        
        <div id="friendsContainer">
            <div class="loading">加载中...</div>
        </div>
    </div>
    
    <script>
        // 页面加载时获取朋友列表
        document.addEventListener('DOMContentLoaded', function() {
            loadFriends();
            loadUserInfo();
        });
        
        // 加载朋友列表
        function loadFriends() {
            fetch('/friend/api/list')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayFriends(data.data);
                    } else {
                        if (data.message === '请先登录') {
                            window.location.href = '/user/login';
                        } else {
                            showError(data.message);
                        }
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showError('加载朋友列表失败');
                });
        }
        
        // 显示朋友列表
        function displayFriends(friends) {
            const container = document.getElementById('friendsContainer');
            
            if (!friends || friends.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <h3>暂无朋友信息</h3>
                        <p>点击"添加朋友"按钮开始添加您的第一个朋友</p>
                        <a href="/friend/create" class="add-btn">添加朋友</a>
                    </div>
                `;
                return;
            }
            
            const friendsHTML = friends.map(friend => `
                <div class="friend-card">
                    <div class="friend-header">
                        <div class="friend-name">${friend.name || '未知姓名'}</div>
                    </div>
                    
                    <div class="friend-info">
                        <div class="info-item">
                            <span class="info-label">邮箱:</span>
                            <span class="info-value">${friend.email || '未填写'}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">电话:</span>
                            <span class="info-value">${friend.phone || '未填写'}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">地址:</span>
                            <span class="info-value">${friend.address || '未填写'}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">生日:</span>
                            <span class="info-value">${friend.birthday || '未填写'}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">学校:</span>
                            <span class="info-value">${friend.school || '未填写'}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">备注:</span>
                            <span class="info-value">${friend.notes || '无'}</span>
                        </div>
                    </div>
                    
                    <div class="friend-actions">
                        <a href="/friend/detail/${friend.id}" class="action-btn view-btn">查看</a>
                        <a href="/friend/edit/${friend.id}" class="action-btn edit-btn">编辑</a>
                        <button class="action-btn delete-btn" onclick="deleteFriend(${friend.id})">删除</button>
                    </div>
                </div>
            `).join('');
            
            container.innerHTML = friendsHTML;
        }
        
        // 搜索朋友
        function searchFriends() {
            const searchTerm = document.getElementById('searchInput').value.trim();
            if (!searchTerm) {
                loadFriends();
                return;
            }
            
            // 直接跳转到搜索结果页面
            window.location.href = `/friend-search.html?name=${encodeURIComponent(searchTerm)}`;
        }
        
        // 删除朋友
        function deleteFriend(id) {
            if (!confirm('确定要删除这个朋友吗？此操作不可恢复。')) {
                return;
            }
            
            fetch(`/friend/delete/${id}`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    loadFriends();
                } else {
                    alert('删除失败：' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('删除失败，请稍后重试');
            });
        }
        
        // 加载用户信息
        function loadUserInfo() {
            // 这里可以从session或API获取用户信息
            // 暂时显示默认值
            document.getElementById('userName').textContent = '当前用户';
        }
        
        // 退出登录
        function logout() {
            if (confirm('确定要退出登录吗？')) {
                window.location.href = '/user/logout';
            }
        }
        
        // 显示错误信息
        function showError(message) {
            const container = document.getElementById('friendsContainer');
            container.innerHTML = `
                <div class="empty-state">
                    <h3>出错了</h3>
                    <p>${message}</p>
                    <button onclick="loadFriends()" class="add-btn">重试</button>
                </div>
            `;
        }
    </script>
</body>
</html>
